package com.bulletelc.apollopre.module.main.center.quotation.detail;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.OrderDataBean;
import com.bulletelc.apollopre.entity.QuotationBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/5/16.
 */

public class QuotationDetailPresenter extends BasePresenter<QuotationDetailContract.View> implements QuotationDetailContract.Presenter {


    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getQuotationDetail(String id) {
        getQuotationDetailImp(id);
    }

    @Override
    public void cancel(int state, int quotationId) {
        cancelImp(state, quotationId);
    }

    @Override
    public void edit(int state, int qutationId) {
        editImp(state, qutationId);
    }

    @Override
    public void submit(int quotationId) {
        submitImp(quotationId);
    }

    private void submitImp(int quotationId) {
        String json = "{\n" +
                "\t\"quotation_id\":" + quotationId + "\n" +
                "}";
        RequestBody requestBody = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.POST_QUOTATION_ORDER_SUBMIT)
                .headers(NetWorkPath.HEADER_KEY_AUTH, NetWorkPath.AUTH_TOKEN_PRE + MyApplication.getToken())
                .upRequestBody(requestBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseSubmit(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorSubmit(response.getException().toString());
                        }
                    }
                });


    }

    private void parseSubmit(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<OrderDataBean>() {
                    }.getType();
                    OrderDataBean orderDataBean = gson.fromJson(object.getJSONObject("data").toString(), type);
                    if (isViewAttached()) {
                        getView().onSuccessSubmit(orderDataBean);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailSubmit(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailSubmit(response.body().toString());
            }
        }
    }

    private void getQuotationDetailImp(String id) {
        OkGo.<String>get(NetWorkPath.POST_QUOTATION_ORDER_DETAIL + "?id=" + id)
                .params("access_token", MyApplication.getToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseDetail(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().onErrorQuotationDetail(response.getException().toString());
                    }
                });

    }

    private void parseDetail(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<QuotationBean>() {
                    }.getType();
                    QuotationBean quotationBean = gson.fromJson(object.getJSONObject("data").toString(), type);
                    if (isViewAttached()) {
                        getView().onSuccessQuotationDetail(quotationBean);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailQuotationDetail(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailQuotationDetail(response.body().toString());
            }
        }
    }

    private void cancelImp(int state, int quotationId) {
        String json = "";
        JSONObject object = new JSONObject();
        try {
            object.put("id", quotationId);
            object.put("state", state);
            json = object.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final RequestBody requestBody = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.POST_QUOTATION_ORDER_SET_STATE)
                .headers(NetWorkPath.HEADER_KEY_AUTH, NetWorkPath.AUTH_TOKEN_PRE + MyApplication.getToken())
                .upRequestBody(requestBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseCancel(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorCancel(response.getException().toString());
                        }
                    }
                });
    }

    private void parseCancel(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    JSONObject data = object.getJSONObject("data");
                    int state = data.getInt("state");
                    if (state == 40) {
                        if (isViewAttached()) {
                            getView().onSuccessCancel();
                        }
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailCancel(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailCancel(response.body().toString());
            }
        }
    }

    private void editImp(int state, int quotationId) {
        String json = "";
        JSONObject object = new JSONObject();
        try {
            object.put("id", quotationId);
            object.put("state", state);
            json = object.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final RequestBody requestBody = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.POST_QUOTATION_ORDER_SET_STATE)
                .headers(NetWorkPath.HEADER_KEY_AUTH, NetWorkPath.AUTH_TOKEN_PRE + MyApplication.getToken())
                .upRequestBody(requestBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseEdit(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorEdit(response.getException().toString());
                        }
                    }
                });
    }


    private void parseEdit(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    JSONObject data = object.getJSONObject("data");
                    int state = data.getInt("state");
                    if (state == 20) {
                        if (isViewAttached()) {
                            getView().onSuccessEdit();
                        }
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailEdit(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailEdit(response.body().toString());
            }
        }
    }


}
